extends Control

var number_textures=[
	preload("res://assets/PNG/Numbers/number0.png"),
	preload("res://assets/PNG/Numbers/number1.png"),
	preload("res://assets/PNG/Numbers/number2.png"),
	preload("res://assets/PNG/Numbers/number3.png"),
	preload("res://assets/PNG/Numbers/number4.png"),
	preload("res://assets/PNG/Numbers/number5.png"),
	preload("res://assets/PNG/Numbers/number6.png"),
	preload("res://assets/PNG/Numbers/number7.png"),
	preload("res://assets/PNG/Numbers/number8.png"),
	preload("res://assets/PNG/Numbers/number9.png")
]

@onready var score_container = %ScoreContainer
@onready var w_game_over_popup = %w_game_over_popup

# 定义退出、重试按钮信号
signal quit_pressed
signal retry_pressed

func _ready():
	# 开始游戏隐藏页面
	w_game_over_popup.hide()
	
# 重试游戏，隐藏页面
func retry_game():
	w_game_over_popup.hide()
	
# 游戏结束，关闭页面
func game_over():
	w_game_over_popup.show()

# 更新分数显示
func update_score_display(current_score:int):
	# 把数字转成字符串，通过符串每位去渲染图片显示
	var score_str:String = str(current_score)
	var digit_length:int = score_str.length()
	for i in range(digit_length):
		var digit = int(score_str[i])
		var digit_sprite:TextureRect
		if score_container.get_child_count() <= i:
			digit_sprite = TextureRect.new()
			score_container.add_child(digit_sprite)
		else:
			digit_sprite = score_container.get_child(i)
		
		digit_sprite.texture = number_textures[digit]
	

# 退出信号
func _on_btn_quit_pressed():
	quit_pressed.emit()

# 重试信号
func _on_btn_retry_pressed():
	retry_pressed.emit()
